﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebNext.Domain.Entities;
using System.Web.Mvc;
using System.Xml.Serialization;
using WebNext.Domain;
using WebNext.UI.Properties;
using WebNext.UI.Utils;

namespace WebNext.UI.Models.Entries
{
	public class EntryHtmlElementViewModel : EntryElementViewModel
	{
		public override EntryElementType Type
		{
			get { return EntryElementType.Html; }
		}

		public string Html
		{
			get;
			private set;
		}

		public bool IsHtmlValid
		{
			get;
			private set;
		}

		public EntryHtmlElementViewModel(EntryHtmlElement entryHtmlElement) : base(entryHtmlElement.Order)
		{
			this.Html = entryHtmlElement.Html;
			this.IsHtmlValid = true;
		}

		public EntryHtmlElementViewModel(int order, IDictionary<string, string> properties)	: base(order)
		{
			if (properties.ContainsKey("html"))
			{
				this.Html = properties["html"];
			}
			this.IsHtmlValid = true;
		}

		public override EntryElement GetEntryElement()
		{
			return new EntryHtmlElement { Html = this.Html, Order = this.Order, Type = this.Type };
		}

		public override ValidationResult Validate()
		{
			ValidationResult result = new ValidationResult();
			if (string.IsNullOrWhiteSpace(this.Html))
			{
				result.AddError(Resources.EntryHtmlCannotBeEmpty);
				this.IsHtmlValid = false;
			}
			else if (this.Html.Length > EntryHtmlElement.MaxHtmlLength)
			{
				result.AddError(Resources.EntryHtmlTooLong, EntryHtmlElement.MaxHtmlLength);
				this.IsHtmlValid = false;
			}
			return result;
		}
	}
}